﻿using Rabbit.Domain;
using Rabbit.Domain.Cultivation;
using Rabbit.DTO.Input;
using Rabbit.DTO.Output;
using Rabbit.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rabbit.Service
{
    public class CultivationService
    {
        /// <summary>
        /// 养殖信息
        /// </summary>
        RabbitRepository<CultivationInfo> _cires;
        /// <summary>
        /// 动检证
        /// </summary>
        RabbitRepository<Dynamic> _dres;
        /// <summary>
        /// 企业信息
        /// </summary>
        RabbitRepository<Enterprise> _eres;
        /// <summary>
        /// 保健信息
        /// </summary>
        RabbitRepository<Immune> _ires;
        /// <summary>
        /// 企业图片
        /// </summary>
        RabbitRepository<EnterpriseImg> _eires;

        public CultivationService(RabbitRepository<CultivationInfo> cires,
            RabbitRepository<Dynamic> dres, RabbitRepository<Enterprise> eres,
            RabbitRepository<Immune> ires, RabbitRepository<EnterpriseImg> eires)
        {
            _cires = cires;
            _dres = dres;
            _eres = eres;
            _ires = ires;
            _eires = eires;
        }


        #region 养殖信息

        /// <summary>
        /// 养殖信息
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<CultivationInfo> GetCultivationInfo(int tmid)
        {
            return _cires.GetAll().Where(x=>x.TMid==tmid).ToList();
        }
        /// <summary>
        /// 添加或修改养殖信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddCultivationInfo(CultivationInfo input)
        {
            var list = _cires.GetAll().FirstOrDefault(x=>x.TMid==input.TMid);
            if (list!=null)
            {
                //修改
                list.CIVariety = input.CIVariety;
                list.CISource = input.CISource;
                list.CIFeedDay = input.CIFeedDay;
                list.CIFeedVariety = input.CIFeedVariety;
                list.CIFeedPeople = input.CIFeedPeople;
                list.CIDate = input.CIDate;
                list.TMid = input.TMid;
                _cires.Updates(list);
                return 1;//修改成功
            }
            _cires.Add(input);
            return 2;//添加成功
        }





        #endregion

        #region 保健信息
        /// <summary>
        /// 显示免疫信息
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<Immune> GetImmune(int tmid)
        {
            return _ires.GetAll().Where(x=>x.TMid==tmid&&x.IsDel!=1).ToList();
        }
        /// <summary>
        /// 添加免疫信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddImmune(Immune input)
        {
            return _ires.Add(input);
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int Del(int ids)
        {
            var list = _ires.GetAll().Where(x => x.ImmuneId == ids).ToList();
            foreach (var item in list)
            {
                item.IsDel = 1;
            }
            return _ires.Update(list);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int UpdImmune(Immune input)
        {
            return _ires.Updates(input);
        }

        #endregion

        #region 动检证

        /// <summary>
        /// 添加动检证
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddDynamic(Dynamic input)
        {
            var list = new Dynamic();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.Durl = input.Durl;
            list.TMid = input.TMid;
            return _dres.Add(list);
        }
        /// <summary>
        /// 图片动检证
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<Dynamic> GetDynamic(int tmid)
        {
            var data = _dres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1).ToList();
            return data;
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelDynamic(int ids)
        {
            var list = _dres.GetAll().Where(x => x.Did == ids).ToList();
            foreach (var item in list)
            {
                item.IsDel = 1;
                item.DelDate = DateTime.Now;
                item.DelName = "老八";
            }
            return _dres.Update(list);
        }

        #endregion


        #region 企业信息

        /// <summary>
        /// 企业信息
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<Enterprise> GetEnterprise(int tmid)
        {
            return _eres.GetAll().Where(x => x.TMid == tmid).ToList();
        }
        /// <summary>
        /// 添加或修改企业信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddEnterprise(Enterprise input)
        {
            var list = _eres.GetAll().FirstOrDefault(x => x.TMid == input.TMid);
            if (list != null)
            {
                //修改
                list.ENum = input.ENum;
                list.EPeople = input.EPeople;
                list.EName = input.EName;
                list.TMid = input.TMid;
                _eres.Updates(list);
                return 1;//修改成功
            }
            _eres.Add(input);
            return 2;//添加成功
        }





        #endregion



        #region 企业图片

        /// <summary>
        /// 添加企业图片
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddEnterpriseImg(EnterpriseImg input)
        {
            var list = new EnterpriseImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.Eurl = input.Eurl;
            list.TMid = input.TMid;
            return _eires.Add(list);
        }
        /// <summary>
        /// 企业图片
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<EnterpriseImg> GetEnterpriseImg(int tmid)
        {
            var data = _eires.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1).ToList();
            return data;
        }
        /// <summary>
        /// 删除企业图片
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelEnterpriseImg(int ids)
        {
            var list = _eires.GetAll().Where(x => x.EIid == ids).ToList();
            foreach (var item in list)
            {
                item.IsDel = 1;
                item.DelDate = DateTime.Now;
                item.DelName = "老八";
            }
            return _eires.Update(list);
        }

        #endregion




    }
}
